// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    loadShow: true,

    //定时器序号
    timer: null,

    searchHotData: [],
    suggestsData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this

    this.getSearchHotData(that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  getSearchHotData(that) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/recipe/search/hot',
      method: "GET",

      success: function (res) {
        that.setData({
          searchHotData: res.data.result.suggests
        })
      },

      complete() {
        that.setData({
          loadShow: false
        })
      }
    })
  },

  getSuggestsData(that, kw) {
    if (kw == '') {
      return
    }

    wx.request({
      url: 'https://apis.netstart.cn/douguo/recipe/search/suggests',
      method: "GET",
      data: {
        kw
      },

      success: function (res) {
        that.setData({
          suggestsData: res.data.result.suggests
        })
      },

      complete() {

      }
    })
  },

  // 监听输入
  watchPassWord(event) {
    var that = this

    //使用函数节流控制触发函数执行的频率
    clearTimeout(that.data.timer);

    that.data.timer = setTimeout(() => {
      const keyword = event.detail.trim()

      that.getSuggestsData(that, keyword)

      if (keyword == '') {
        that.setData({
          suggestsData: []
        })
      }
    }, 500)
  },

  onClickJump(event) {
    const kw = event.currentTarget.dataset.kw

    wx.navigateTo({
      url: '../searchpages/searchpages?kw=' + kw
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})